package com.ysd.modules.sys.service.impl;


import com.ysd.common.exception.RRException;
import com.ysd.modules.sys.dao.SysStudentDao;
import com.ysd.modules.sys.dao.SysTeacherDao;
import com.ysd.modules.sys.entity.*;
import com.ysd.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.utils.PageUtils;
import com.ysd.modules.sys.dao.SysClassesDao;
import com.ysd.modules.sys.service.SysClassesService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysClassesServiceImpl extends ServiceImpl<SysClassesDao, SysClassesEntity> implements SysClassesService {
    @Resource
    private SysStudentDao sysStudentDao;
    @Resource
    private SysTeacherDao sysTeacherDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysClassesDao sysClassesDao;

    @Override
    public PageUtils getAllClasses(String cname, Integer page, Integer limit) {
        Page<SysClassesEntity> pageInfo = new Page<SysClassesEntity>(page, limit);
        QueryWrapper<SysClassesEntity> wapper = new QueryWrapper<SysClassesEntity>();
        if (cname != null && cname != "") {
            wapper.like("name", cname);
        }
        return new PageUtils(this.page(pageInfo, wapper));
    }

    @Override
    public int addClasses(String cname, Integer sid) {
        SysClassesEntity c = new SysClassesEntity();
        c.setName(cname);
        c.setSchoolId(sid);
        return baseMapper.insert(c);
    }

    @Override
    public int updateClasses(SysClassesEntity c) {
        return baseMapper.updateById(c);
    }

    @Override
    public int delClasses(Integer cid) {
        return baseMapper.deleteById(cid);
    }

    /**
     * 根据班级名分页查询班级
     *
     * @param searchName
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageUtils getAllStudents(String searchName, Integer page, Integer limit) {
        Page<SysStudent> pageInfo = new Page<>(page, limit);
        QueryWrapper<SysStudent> wapper = new QueryWrapper<>();
        if (searchName != null && searchName != "") {
            wapper.like("name", searchName);
        }
        return new PageUtils(sysStudentDao.selectPage(pageInfo, wapper));
    }

    /**
     * 添加学生
     *
     * @param sysStudent
     * @return
     */
    @Override
    public int addStudents(SysStudent sysStudent) {
        return sysStudentDao.insert(sysStudent);
    }

    /**
     * 修改学信息
     *
     * @param sysStudent
     * @return
     */
    @Override
    public int updateStudents(SysStudent sysStudent) {
        return sysStudentDao.updateById(sysStudent);
    }

    /**
     * 删除学生
     *
     * @param id
     * @return
     */
    @Override
    public int delStudents(Integer id) {
        return sysStudentDao.deleteById(id);
    }

    /**
     * 增加教师
     * 增加教师的同时需要增加一个user
     *
     * @param sysTeacher
     * @return
     */
    @Override
    public int addTeacher(SysTeacher sysTeacher) {
        SysUserEntity user = new SysUserEntity();
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
        user.setSalt(salt);
        user.setUsername(sysTeacher.getAccount());
        user.setEmail(sysTeacher.getAccount() + "@163.com");
        user.setMobile(sysTeacher.getPhone());
        user.setStatus(1);
        user = sysUserService.saveUser(user);
        if (user == null || user.getUserId() == 0) {
            throw new RRException("新增用户时未返回当前对象");
        }
        sysTeacher.setUserId(user.getUserId().intValue());
        return sysTeacherDao.insert(sysTeacher);
    }

    /**
     * 修改老师信息
     *
     * @param sysTeacher
     * @return
     */
    @Override
    public int updateTeacher(SysTeacher sysTeacher) {
        if (StringUtils.isBlank(sysTeacher.getPwd())) {
            sysTeacher.setPwd(null);
        } else {
            sysTeacher.setPwd(new Sha256Hash(sysTeacher.getPwd(), sysTeacher.getSalt()).toHex());
        }

        SysUserEntity user = new SysUserEntity();
        user.setPassword(sysTeacher.getPwd());
        user.setSalt(sysTeacher.getSalt());
        user.setUsername(sysTeacher.getAccount());
        user.setEmail(sysTeacher.getAccount() + "@163.com");
        user.setMobile(sysTeacher.getPhone());
        user.setStatus(1);
        user.setUserId(sysTeacher.getUserId().longValue());
        sysUserService.updateById(user);
        return sysTeacherDao.updateById(sysTeacher);
    }

    /**
     * 删除老师
     *
     * @param id
     * @return
     */
    @Override
    public int delTeacher(Integer id) {
        SysTeacher sysTeacher = sysTeacherDao.selectById(id);
        if (sysTeacher == null) {
            throw new RRException("未找到老师信息");
        }
        Long[] userIds = {sysTeacher.getUserId().longValue()};
        sysUserService.deleteBatch(userIds);
        return sysTeacherDao.deleteById(id);
    }

    /**
     * 根据老师名查询老师
     *
     * @param searchName
     * @return
     */
    @Override
    public List<SysTeacherVO> getAllTeacher(String searchName) {
        List<SysTeacherVO> sysTeacherVOS = new ArrayList<>();
        QueryWrapper<SysTeacher> st = new QueryWrapper<>();
        if (!StringUtils.isBlank(searchName)) {
            st.eq("account", searchName);
        }
        List<SysTeacher> sysTeacherList = sysTeacherDao.selectList(st);
        for (int i = 0; i < sysTeacherList.size(); i++) {
            sysTeacherVOS.get(i).setSysTeacher(sysTeacherList.get(i));
            SysTeacher sysTeacher = sysTeacherList.get(i);
            SysUserEntity sysUserEntity = sysUserService.getById(sysTeacher.getUserId());
            sysTeacherVOS.get(i).setSysUserEntity(sysUserEntity);
        }
        return sysTeacherVOS;
    }

	@Override
	public PageUtils getClassByParams(Map<String, Object> params) {
		Integer page = params.get("page") == null ? 1 : (Integer) params.get("page");
		Integer limit = params.get("limit") == null ? 10 : (Integer) params.get("limit");
    	Page<SysClassesEntity> pageInfo = new Page<SysClassesEntity>(page,limit);
    	Page<SysClassesEntity> pageData = sysClassesDao.getClassByParams(params,pageInfo);
		return new PageUtils(pageData);
	}

	@Override
	public List<SysTeacher> getTeacherAll() {
		return sysTeacherDao.selectList(null);
	}

	@Override
	public List<SysClassesEntity> GetBySchoolId(int schoolId) {
		// TODO Auto-generated method stub
		return baseMapper.getBySchoolId(schoolId);
	}

}